<?php
/**
 * Zead
 *
 * LICENSE	
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to eu@marcelomx.com so we can send you a copy immediately.
 *
 * @category   Zead
 * @author     Marcelo Rodrigues Gonzaga <eu@marcelomx.com>
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga
 * @link       http://code.google.com/p/zead 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License
 *
 **/


/** @see Zead_Manifest **/
require_once 'Zead/Manifest.php';

/** @see Zead_Manifest_Adapter_Abstract **/
require_once 'Zead/Manifest/Adapter/Abstract.php';

/** @see Zead_Manifest_Adapter_Db_Objects **/
require_once 'Zead/Manifest/Adapter/Db/Objects.php';

/** @see Zead_Manifest_Adapter_Db_Contents **/
require_once 'Zead/Manifest/Adapter/Db/Contents.php';

/** @see Zead_Manifest_Adapter_Db_Options **/
require_once 'Zead/Manifest/Adapter/Db/Options.php';

/**
 * Class Zead_Manifest_Adapter_Db
 * 
 * @category   Zead
 * @package    Zead_Manifest
 * @subpackage Zead_Manifest_Adapter
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License    
 */
class Zead_Manifest_Adapter_Db extends Zead_Manifest
{
	/** @var Zend_Db_Adapter_Abstract **/
	protected $_adapter = null;

	/**
	 * In this adapter, the config params are:
	 *
	 * adapter  = MYSQL, SQL, ORACLE, POSTGRESQL, etc.
	 * host     = localhost
	 * username = root
	 * password = ****
	 * dbname   = zead
	 * id       = string The id of resource manifest accessed.
	 */
	protected function _setup(array $config = array())
	{
		/** @see Zend_Db **/
		require_once 'Zend/Db.php';
		// Connect to database
		$this->_adapter = Zend_Db::factory($config['adapter'], $config);
		
		// If has no default adapters, set this adapter as default
		if (Zend_Db_Table::getDefaultAdapter() === null) {
			Zend_Db_Table::setDefaultAdapter($this->_adapter);
		}
		// Retrieving the objects
		$objectsTable = new Zead_Manifest_Adapter_Db_Objects(array('adapter' => $this->_adapter));
		
		// If has no object id
		if (!isset($config['id']))
			return;
			
		// Retrieve the object row
		$objectRow = $objectsTable->fetchRow($objectsTable->select()->where('id = ?', $config['id']));
		
		// If object not finded	
		if (null === $objectRow)
			return;

		// Sets the manifest title
		$this->_manifest->setTitle($objectRow->title);
			
		// Retrieve contents
		$contents = $objectRow->findZead_Manifest_Adapter_Db_Contents();
		
		// Parse contents
		foreach ($contents as $content) {
			$this->_manifest->addContent($content->name, $content->title);
		}
		
		// Retrieve options
		$options  = $objectRow->findZead_Manifest_Adapter_Db_Options();
		
		// Parse options
		foreach ($options as $option) {
			$this->_manifest->setOption($option->namespace, $option->value);
		}
	}
}
